package com.kkd.kkdapp.db.user;

import com.kkd.kkdapp.BaseApplication;
import com.kkd.kkdapp.activity.LoginActivity;
import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.db.app.model.Notice;
import com.kkd.kkdapp.db.base.BaseEASQLiteOpenHelper;
import com.kkd.kkdapp.db.base.DataObservable;
import com.kkd.kkdapp.db.base.SqlBuilder;
import com.kkd.kkdapp.db.user.model.CrediItemTab;
import com.kkd.kkdapp.db.user.model.KkdMessageModel;
import com.kkd.kkdapp.db.user.model.PrivilegeModel;
import com.kkd.kkdapp.db.user.model.UserInfo;
import com.kkd.kkdapp.utils.LogUtil;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

/**
 * 登陆用户对应的信息数据库创建
 * 
 * @author askzhao
 * 
 */
public class KkdDBHelper extends BaseEASQLiteOpenHelper {

	/** 数据库版本 */
	private static final int DB_VERSION = 6;

	private static KkdDBHelper instance;
	public DataObservable onDBCreatedObservable = new DataObservable();

	/** 调用之前必须保证已经调用过init方法 */
	public static synchronized KkdDBHelper getInstance() {
		if (instance == null) {
			if (CurUserModel.getCurUser() != null) {
				init(BaseApplication.getInstance(),
						CurUserModel.getCurUser().customerId);
			} else {
				init(BaseApplication.getInstance(), "086");// 创建这个只是为了不崩溃,为下面重新登陆做准备
				// Intent intent = new Intent(BaseApplication.getInstance(),
				// LoginActivity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
				// BaseApplication.getInstance().startActivity(intent);
			}
		}
		return instance;
	}

	/**
	 * 实例化当前用户的数据库
	 * 
	 * @param context
	 * @param customerId
	 */
	public static synchronized void init(Context context, String customerId) {
		String dbname = "kkd_" + customerId + ".db";
		if (instance != null) {
			instance.close();
		}
		instance = new KkdDBHelper(context, dbname, null, DB_VERSION);

	}

	public synchronized void closeDB() {
		if (instance != null) {
			instance.close();
		}
	}

	private KkdDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(final SQLiteDatabase db) {
		super.onCreate(db);
		LogUtil.d("EADBHelper", "onCreate ");
		createAllTables(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		super.onUpgrade(db, oldVersion, newVersion);
		LogUtil.d("EADBHelper", "onUpgrade " + oldVersion + ", " + newVersion);
		// 下面是更新数据库的示例
		if (oldVersion < 6) {
			dropAllTables(db);
			createAllTables(db);
		}

	}

	private void createAllTables(final SQLiteDatabase db) {
		db.execSQL(SqlBuilder.getCreateTableSql(UserInfo.class));
		db.execSQL(SqlBuilder.getCreateTableSql(CrediItemTab.class));
		db.execSQL(SqlBuilder.getCreateTableSql(KkdMessageModel.class));
		db.execSQL(SqlBuilder.getCreateTableSql(PrivilegeModel.class));
	}

	private void dropAllTables(final SQLiteDatabase db) {
		db.execSQL(SqlBuilder.getDropTableSql(UserInfo.class));
		db.execSQL(SqlBuilder.getDropTableSql(CrediItemTab.class));
		db.execSQL(SqlBuilder.getDropTableSql(KkdMessageModel.class));
		db.execSQL(SqlBuilder.getDropTableSql(PrivilegeModel.class));
		
	}

}
